$(function(){
	loadItems();	//页面加载扣款项下拉列表
	loadStore();	//页面加载初始化门店数据
	loadTaxRateCode();	//页面加载初始化税率
	clickStore();
	if($("#taxTypeCode").val() == null){
		loadSupplier();
	}
	
	$("#areaCoefficient").decimalinput();
	$("#contractArea").decimalinput();
	$("#benefit").decimalinput();
	$("#salesAmount").decimalinput();
	$("#isTrainingAmount").decimalinput();
	$("#isJobCardAmount").decimalinput();
	
	// 选择地区
	$("#save").click(function(){
		var flag = validataAdd();
		var negType = $("#negType").val();
		var mainCount=0;
		var navId = $("#navId").val();
		var positionLog = $("#positionLog").val();
		if(flag != false){			//如何表单验证通过则提交表单
			
			if($("#negAssBrands").find("tr").length<=1){
				top.$.jBox.tip('必须录入品牌', 'warning');
				return false;
			}
			try {
				for (var int = 0; int < assBrandList.length; int++) {
					var shopId=$('#assShopIds'+assBrandList[int]).val();
					var mainBarnd=$('#mainBarnd'+assBrandList[int]).val();
					if(shopId==''){
						throw "品牌信息中第-"+Number(int+1)+"-行商铺不能为空";
					}
					if(mainBarnd==1){
						mainCount=mainCount+1;
					}
				}
			}catch (e) {
	            top.$.jBox.tip(e, 'warning');
	            return;
			}
			
			if(mainCount==0){
				top.$.jBox.tip('品牌信息中必须要有主品牌', 'warning');
				return false;
			}
			if(mainCount>1){
				top.$.jBox.tip('品牌信息中主品牌只能有一个', 'warning');
				return false;
			}
			
			if(negType == "40"){		//disabled后，Controller不能获得页面的值。 这里在保存操作前先移除disabled属性
				$("input").removeAttr("disabled");
				$("input[type='radio']").removeAttr("disabled");
				$("select").removeAttr("disabled");
			}
			$('#assrecordsForm').method = "post";
			$('#assrecordsForm').attr('action',contextpath + '/biz/contract/assrecord/save?positionLog='+positionLog+'&navId='+navId);
 			$("#assrecordsForm").submit();
		}
	});

	//区域
	var opt = {
			service : "selectOneRegionService",
			idField : "areaId",
			nameField : "area",
			title : "地区选择对话框"
	};
	$("#areaDialog").Dialog(opt);
	
//linzhizhong 失去焦点会更新门店值
	//选择区域时。 根据区域获得门店信息
//	$("#area").blur(
//		function() {
//			var areaId = $("#areaId").val();
//			$.get(contextpath + '/biz/contract/commonContract/' + areaId
//				+ '/getStoreByAreaId',
//				function(data) {
//					var jsonObj = eval('(' + data + ')');
//					$("#storeId").children("option").each(function() {
//						$(this).remove();
//					});
//					$("#storeId").append(
//							"<option value=''>---请选择---</option>");
//					for (var i = 0; i < jsonObj.length; i++) {
//						var shop = jsonObj[i];
//						$("#storeId").append(
//								"<option value='" + shop.code + "'>"
//										+ shop.name + "</option>");
//					}
//			});
//	});
//	
	$("#storeId").change(
		function() {
			clickStore();
			$("#store").val(store);
			$("#storeId_hid").val(storeId);
			if (storeId != "" && storeId != null) {
				$.get(contextpath + '/biz/contract/commonContract/'
						+ storeId + '/getFloorByStoreId', function(data) {
					var floorList = eval('(' + data + ')');
					$("#floorId").children("option").each(function() {
						$(this).remove();
					});
					$("#floorId").append(
							"<option value=''>---请选择---</option>");
					for (var i = 0; i < floorList.length; i++) {
						var floor = floorList[i];
						$("#floorId").append(
								"<option value='" + floor.code + "'>"
										+ floor.name + "</option>");
					}
				});
			}
		});
	
	//选择楼层时。根据门店获得楼层信息
	$("#floorId").change(
		function() {
			var floorId = $("#floorId").val();
			var floor;
			$(this).children("option").each(function() {
				if ($(this).val() == floorId) {
					floor= $(this).text();
				}
			});
			$("#floor").val(floor);
			$("#floor_hid").val(floorId);
		});
	
	$("#shopDialog").click(function() {
		var floorId = $("#floorId").attr("value");
		var storeId = $("#storeId").attr("value");
		if (floorId == null || floorId == '') {
			$("#floor").focus();
			top.$.jBox.tip('楼层不能为空', 'warning');
			return false;
		} else {
			$("#modalBrand").load(contextpath+ "/biz/contract/commonContract/showModal?floor="+ floorId + "&siteid="+ storeId + "&pageSize=10",
			function() {

				$("#modalBrand").modal("show");
			});
		}
	});
	
	//供应商选择
	$("#supplier").click(function() {
		$("#supplierModal").load(contextpath+ '/biz/brand/supplier/suppInfoPage?pageSize=10',function() {
				$("#supplierModal").modal("show");
		});
	});
	
	// 经营门店 多选对话框
	var selectStoreDialog = {
		service : "selectShopByRegionService",
		idField : "selfStore_storeNo",
		nameField : "selfStore_store",
		multiple : "true",
		title : "门店选择对话框"
	};
	$("#selectStoreDialog").Dialog(selectStoreDialog);// 经营门店 多选对话框
	var isModifyContract =$("#isModifyContract").val();
	var negType = $("#negType").val();
	if(negType == "40" && isModifyContract!="1"){
		$("input").attr("disabled", "disabled");
		$("#selectStoreDialog").attr("disabled", "disabled");
		$("select").attr("disabled", "disabled");
		$(".delete").removeAttr("onclick");
		$(".delete").css("color","#e8e8e8");
		$("#guideType").removeAttr("disabled");
		$("#guideAmount").removeAttr("disabled");
		
	}
	//变更和续签可以修改楼层
	if(isModifyContract=='1'||negType=='30'){
		$("#floorId").removeAttr("disabled");
	}
	
	
	
});